<?php

namespace App\Models\Factory;

use App\Models\AbsModelFactory;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;

/**
 * 缓存存取工厂方法
 * Class CacheFactory
 * @package App\Models\Factory
 * Cache
 */
class CacheFactory extends AbsModelFactory
{

    // cache存储  7天
    public static function putValueToCache($key, $value,$second)
    {
        return Cache::put($key, $value, Carbon::now()->second($second));
    }

    //永久存储
    public static function putValueToCacheForever($key, $value)
    {
        return Cache::forever($key, $value, Carbon::now());
    }

    //存储7200秒
    public static function putValueToCacheTwoMinutes($key, $value)
    {
        return Cache::put($key, $value, Carbon::now()->second(7200));
    }

    // 从cache 读取数据
    public static function getValueFromCache($key)
    {
        return Cache::get($key);
    }

    // cache 中数据是否存在
    public static function existValueFromCache($key)
    {
        return Cache::get($key) ? true : false;
    }


    /**
     * 用户登录输入密码错误后的限制
     * @param $key
     * @param $second
     */
    public static function  loginLimit($key,$second)
    {
        $num = Cache::get($key);

        if ($num == 5) {
            Cache::put($key.'_limit',$key,Carbon::now()->second(30*60));
        }

        if ($num) {

            Cache::put($key,Cache::get($key) +1);

        }else {

            Cache::put($key,1, Carbon::now()->second($second));

        }

    }


}
